package com.stlm2.controller;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.entity.GoodsType;
import com.stlm2.dems.entity.IntegralGoods;
import com.stlm2.dems.entity.Periods;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.IntegralGoodsService;
import com.stlm2.dems.service.PeriodsService;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Author：fuchunyu
 * Date：2017/12/20
 * Description：积分商品控制器
 */
@Controller
@RequestMapping("/integralGoods")
public class IntegralGoodsController {

    @Resource
    private UserService userService;
    @Resource
    private IntegralGoodsService integralGoodsService;
    @Resource
    private PeriodsService periodsService;



    /**
     * @param page   页数
     * @return 积分商品查询
     */
    @ResponseBody
    @RequestMapping(value = "/search.json", produces = "application/json;charset=utf-8")
    public String search(String companyCode,ModelMap model, String page,Integer goodsTypeValue) {

        int pageNo = 1;
        int pageSize = 20;
        if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
            pageNo = NumberUtils.toInt(page);
        }


        if(GoodsType.IntegralPrize.getIntValue()-goodsTypeValue==0){
            Periods periods=new Periods();
            periods.setState(1);
            PageView pageview = periodsService.getPage(pageNo, pageSize, periods);
            if (pageview != null) {
                model.put("datas", periodsService.convertModel(pageview.getRecords()));
                model.put("recordsTotal", pageview.getRowCount());
                model.put("totalPage", pageview.getPageCount());
                model.put("pageNow", pageview.getPageNow());
            }
        }else {
            IntegralGoods integralGoods = new IntegralGoods();
            integralGoods.setState(1);
            integralGoods.setGoodsTypeValue(goodsTypeValue);
            PageView pageview = integralGoodsService.getPage(pageNo, pageSize, integralGoods);
            if (pageview != null) {
                model.put("datas", integralGoodsService.convertModel(pageview.getRecords()));
                model.put("recordsTotal", pageview.getRowCount());
                model.put("totalPage", pageview.getPageCount());
                model.put("pageNow", pageview.getPageNow());
            }
        }

        return CodeContent.returnSuccess(model);
    }


    /**
     *
     * @param id   商品ID
     * @return  订单详情
     */
    @ResponseBody
    @RequestMapping(value = "/detail.json", produces = "application/json;charset=utf-8")
    public String detail(String companyCode,Integer id,Integer goodsTypeValue,ModelMap modelMap){

        if(GoodsType.IntegralPrize.getIntValue()-goodsTypeValue==0){
            Periods periods=periodsService.findEntityById(id);
            if(periods==null)
                return CodeContent.returnFailure("订单查询失败！");
            modelMap=periodsService.buildModel(periods);
        }else {
            IntegralGoods integralOrder = integralGoodsService.findEntityById(id);
            if (integralOrder == null)
                return CodeContent.returnFailure("订单查询失败！");
             modelMap = integralGoodsService.buildModel(integralOrder);
        }
        return CodeContent.returnSuccess(modelMap);
    }

}
